//
//  PersonalResumeVC.m
//  FindJob
//
//  Created by 233 on 16/2/29.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "PersonalResumeVC.h"
#import "ResumeCell.h"
#import "ResumeDetailVC.h"

#import "PersonalModel.h"
#import "WorkingOfExperienceModel.h"
#import "EducationOfExperienceModel.h"
#import "JobOfIntentionModel.h"
#import "TrainOfExperienceModel.h"
#import "LanguageOfAbilityModel.h"


static NSString *const kResumeCell = @"resumeCell";
static NSString *const kResumeDetailVC = @"ResumeDetailVC";
@interface PersonalResumeVC ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSInteger _tag;
    
    UIImagePickerController *_imagePickerController; /**< 相册拾取器 */

}
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSArray *dataSource;
@property (nonatomic, copy) NSArray *dataModel;
- (void)initializeUserIntefcace;  /**< 初始化用户界面 */
- (void)initializeDataSource;     /**< 初始化数据源 */
@end

@implementation PersonalResumeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserIntefcace];
}
- (void)initializeDataSource{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"resume" ofType:@"plist"];
    _dataSource = [NSArray arrayWithContentsOfFile:path];
}
- (void)initializeUserIntefcace{
    
    UIGestureRecognizer *gesture = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(respondsToHeadImageViewGesture:)];
    
    // 初始化相册选择器
    _imagePickerController = [[UIImagePickerController alloc] init];
    // 设置代理
    _imagePickerController.delegate = self;
    
    
    [self.headImageView addGestureRecognizer:gesture];
    UINib *resumeCell = [UINib nibWithNibName:NSStringFromClass([ResumeCell class]) bundle:nil];
    [self.tableView registerNib:resumeCell forCellReuseIdentifier:kResumeCell];
}

#pragma mark - gesture


- (IBAction)respondsToHeadBtn:(UIButton *)sender {
    [self selectPhotoAlbumPhotos];
}

- (void)respondsToHeadImageViewGesture:(UIGestureRecognizer *)gesture{
    // 弹出表单
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"上传头像"
//                                                                             message:nil
//                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
//    // 相册选择
//    [alertController addAction:[UIAlertAction actionWithTitle:@"照片" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        [self selectPhotoAlbumPhotos];
//    }]];
    [self selectPhotoAlbumPhotos];

}

#pragma mark - privet methods
// 选择相册照片
- (void)selectPhotoAlbumPhotos {
    // 获取支持的媒体格式
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    // 判断是否支持需要设置的sourceType
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        // 1、设置图片拾取器上的sourceType
        _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 2、设置支持的媒体格式
        _imagePickerController.mediaTypes = @[mediaTypes[0]];
        // 3、其他设置
        _imagePickerController.allowsEditing = YES; // 如果设置为NO，当用户选择了图片之后不会进入图像编辑界面。
        // 4、推送图片拾取器控制器
        [self presentViewController:_imagePickerController animated:YES completion:nil];
    }
}

#pragma mark *** UIbutton action ***
- (IBAction)unwindToMainViewController:(UIStoryboardSegue *)unwindSegue{
    
}
#pragma mark - <UITableViewDataSource,UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ResumeCell *cell = [tableView dequeueReusableCellWithIdentifier:kResumeCell];
    cell.titleNameLabel.text = _dataSource[indexPath.row];
    cell.BoolLabel.text = @"不完整";
//    cell.layer.cornerRadius
//    cell.layer.masksToBounds
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        PersonalModel *model = [[PersonalModel alloc] init];
        _dataModel = [model gainPersonalInfoModel];
         _tag = indexPath.row + 10;
    }else if (indexPath.row == 1){
        WorkingOfExperienceModel *model = [[WorkingOfExperienceModel alloc] init];
        _dataModel = [model gainWorkingOfExperience];
         _tag = indexPath.row + 10;
    }else if (indexPath.row == 2){
        EducationOfExperienceModel *model = [[EducationOfExperienceModel alloc] init];
        _dataModel = [model gainEducationOfExperienceModel];
         _tag = indexPath.row + 10;
    }else if (indexPath.row == 3){
        JobOfIntentionModel *model = [[JobOfIntentionModel alloc] init];
        _dataModel = [model gainJobOfIntentionModel];
         _tag = indexPath.row + 10;
    }else if (indexPath.row == 4){
        TrainOfExperienceModel *model = [[TrainOfExperienceModel alloc] init];
        _dataModel = [model gainTrainOfExperienceModel];
         _tag = indexPath.row + 10;
    }else{
        LanguageOfAbilityModel *model = [[LanguageOfAbilityModel alloc] init];
        _dataModel = [model gainLanguageOfAbilityModel];
         _tag = indexPath.row + 10;
    }
   
    [self performSegueWithIdentifier:kResumeDetailVC sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController isKindOfClass:[ResumeDetailVC class]]) {
        ResumeDetailVC  *rdVc = segue.destinationViewController;
        rdVc.titleName = _dataSource[self.tableView.indexPathForSelectedRow.row];
        rdVc.datas = _dataModel;
        rdVc.tag = _tag;
    }
}

#pragma mark - <UIImagePickerControllerDelegate> & <UINavigationControllerDelegate>
// 用户选择了某个媒体
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSLog(@"User chosed imageView media with info '%@'.", info);
    
//    _uploadButton.hidden = NO;
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        if ([info[UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
            UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
            _headImageView.image = originalImage;
        }
    }else if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
        // UIImage *editedImage = info[@"UIImagePickerControllerEditedImage"];
        // _headPortraitImageView.image = editedImage;
        
        _headImageView.image = info[UIImagePickerControllerEditedImage];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 用户点击了取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    
    // 显示按钮
//    _uploadButton.hidden = NO;
    
    // 消失相册
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}

@end
